import * as ordersService from '../services/orders';

export default {
  namespace: 'orders',
  state: {
    order: null,
    modal: { visible: false },
    selectedRowKeys: [],
    list: [],
    total: null
  },

  reducers: {
    save(state, { payload }) {
        return Object.assign(state, payload);
    },

    multiSelect(state, { selectedRowKeys }) {
        return { ...state, selectedRowKeys };
    }
  },

  effects: {

    // 请求列表
    *fetch({ payload }, { call, put }) {
        const { data, total, current_page } = yield call(ordersService.fetch, payload);
        yield put({ 
            type: 'save', 
            payload: {
                selectedRowKeys: [],
                list: data, 
                total: total,
                page: parseInt(current_page, 10) || 1
            } 
        });
    },


    // 弹出新增窗口
    *add({}, {call, put, select}) {
        yield put({ 
            type: 'save', 
            payload: {
                order: {},
                modal: {
                    action: 'add',
                    visible: true
                }
            } 
        });
    }, 


    // 新建数据
    *create({ payload: { values } }, { call, put, select }) {
        const result = yield call(ordersService.post, { values });
        if (result && result.status=='ok') {
            yield put({ type: 'hideModal' });
            yield put({ type: 'refresh' });
        }
    },


    // 弹出修改窗口
    *edit({ payload: { id } }, {call, put, select}) {
        const order = yield call(ordersService.find, { id });
        yield put({ 
            type: 'save', 
            payload: {
                order: order,
                modal: {
                    action: 'edit',
                    visible: true
                }
            } 
        });
    },


    // 更新资料
    *patch({ payload: { id, values } }, { call, put, select }) {
        const result = yield call(ordersService.patch, { id, values });
        if (result && result.status=='ok') {
            yield put({ type: 'hideModal' });
            yield put({ type: 'refresh' });
        }
    },


    // 删除单项数据
    *remove({ payload: { id } }, { call, put, select }) {
        const result = yield call(ordersService.remove, { id });
        if (result && result.status=='ok') {
            yield put({ type: 'refresh' });
        }
    },


    // 批量删除数据
    *batchRemove({}, { call, put, select }) {
        const id = yield select(state => state.orders.selectedRowKeys);
        const result = yield call(ordersService.batchRemove, { id });
        if (result && result.status=='ok') {
            yield put({ type: 'refresh' });
        }
    },


    // 刷新列表
    *refresh({}, { put, select }) {
        const query = yield select(state => state.routing.locationBeforeTransitions.query);
        yield put({ type: 'fetch', payload: query });
    },


    // 隐藏窗口
    *hideModal({}, {call, put, select}) {
        yield put({ 
            type: 'save', 
            payload: {
                modal: {
                    visible: false
                }
            } 
        });
    }

  },

  subscriptions: {
    setup({ dispatch, history }) {
        return history.listen(({ pathname, query }) => {
            if (pathname === '/orders') {
                dispatch({ type: 'fetch', payload: query });
            }
        })
    }
  },
};
